{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[150],\n",
       "       [200],\n",
       "       [250],\n",
       "       [300],\n",
       "       [350],\n",
       "       [400],\n",
       "       [600]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 特征必须是二维的。 7行一列\n",
    "X = np.array([150, 200, 250, 300, 350, 400, 600])\n",
    "X = X.reshape(7,1)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1., 150.],\n",
       "       [  1., 200.],\n",
       "       [  1., 250.],\n",
       "       [  1., 300.],\n",
       "       [  1., 350.],\n",
       "       [  1., 400.],\n",
       "       [  1., 600.]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_new = np.hstack( (np.ones((7,1)), X) )\n",
    "X_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[  1., 150.],\n",
       "        [  1., 200.],\n",
       "        [  1., 250.],\n",
       "        [  1., 300.],\n",
       "        [  1., 350.],\n",
       "        [  1., 400.],\n",
       "        [  1., 600.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_mat = np.mat(X_new)\n",
    "# print(\"新的特征\\n\", X_new)\n",
    "x_mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 6450],\n",
       "        [ 7450],\n",
       "        [ 8450],\n",
       "        [ 9450],\n",
       "        [11450],\n",
       "        [15450],\n",
       "        [18450]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标签的可以一维的\n",
    "y = np.array([6450, 7450, 8450, 9450, 11450, 15450, 18450]).reshape(7,1)\n",
    "y_mat = np.mat(y)\n",
    "y_mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[7.000e+00, 2.250e+03],\n",
       "        [2.250e+03, 8.575e+05]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exp1 = (x_mat.T * x_mat)\n",
    "exp1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 9.12234043e-01, -2.39361702e-03],\n",
       "        [-2.39361702e-03,  7.44680851e-06]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exp2 = exp1.I  #求逆\n",
    "exp2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 5.53191489e-01,  4.33510638e-01,  3.13829787e-01,\n",
       "          1.94148936e-01,  7.44680851e-02, -4.52127660e-02,\n",
       "         -5.23936170e-01],\n",
       "        [-1.27659574e-03, -9.04255319e-04, -5.31914894e-04,\n",
       "         -1.59574468e-04,  2.12765957e-04,  5.85106383e-04,\n",
       "          2.07446809e-03]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exp3 = exp2* x_mat.T\n",
    "exp3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "回归系数 [[1771.80851064]\n",
      " [  28.77659574]]\n"
     ]
    }
   ],
   "source": [
    "w = exp3 * y\n",
    "print(\"回归系数\", w)  # 系数和截距"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# y = 28.77659574 * x + 1771.80851064"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
